﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <libxl.h>

// 订水信息结构体
struct water_info {
    char timestamp[200]; 
    char name[20];  // 姓名
    char address[20]; //地址
    char phone[20]; // 联系电话
    int amount;     // 订购数量
    float money;      // 订水金额
};

// 全局变量，存储订水信息
struct water_info water_infos[100];
int num_water_infos = 0; // 已有的订水信息数量

// 函数声明
void InputInfo();                        // 输入
void BrowseInfo();                       // 浏览
void QueryInfo();                        // 查询
void ModifyInfo();                       // 修改
void DeleteInfo();                       // 删除
void TimeInfo(struct water_info* info);   // 时间
int Login();                                    // 登录


void Menu(int current, int max_choice) {
    printf("\n");
    for (int i = 1; i <= max_choice; i++) {
        printf("                                                  %c ", i == current ? '>' : ' ');  // 如果是当前选中项，加上箭头
        printf(" %d. ", i);
        switch (i) {
        case 1:
            printf("信息录入");
            break;
        case 2:
            printf("信息浏览");
            break;
        case 3:
            printf("信息查询");
            break;
        case 4:
            printf("信息修改");
            break;
        case 5:
            printf("信息删除");
            break;
        case 6:
            printf("退出程序");
            break;
        }
        printf("\n");
    }

}

// 保存数据到文件
void SaveData() {
    FILE* fp = fopen("water_info.dat", "wb");
    if (fp == NULL) {
        printf("\n系统检测到无历史记录，请继续！\n");
        system("pause");
        return;
    }

    fwrite(&num_water_infos, sizeof(int), 1, fp);
    fwrite(water_infos, sizeof(struct water_info), num_water_infos, fp);
    
    fclose(fp);
}    

// 从文件中读取数据
void LoadData() {
    FILE* fp = fopen("water_info.dat", "rb");
    if (fp == NULL) {
        printf("\n无法打开文件或文件丢失！\n");
        system("pause");
        return;
    }

    fread(&num_water_infos, sizeof(int), 1, fp);
    fread(water_infos, sizeof(struct water_info), num_water_infos, fp);

    fclose(fp);
}


// 主函数
int main() {
    while (Login());
    LoadData(); // 读取之前保存的数据
    int current = 1;
    int max_choice = 6;

    while (1)
    {

        // 显示主菜单
        printf("\n%*s======================== 欢迎使用订水程序 ========================\n",30,"");
        Menu(current, max_choice);

        // 读取键盘输入
        int ch = getch();

        
            if (ch == 224) {
                
                // 如果读到了扩展键码，则再读取一次获取具体的键码
                ch = getch();
                if (ch == 72) {  // 上箭头
                    system("cls");
                    current = current == 1 ? max_choice : current - 1;  // 循环滚动选择项
                }
                else if (ch == 80) {  // 下箭头
                    system("cls");
                    current = current == max_choice ? 1 : current + 1;  // 循环滚动选择项
                }
                if(ch!=72&&ch!=80)
                {
                    system("cls");
                    printf("\n\n%*s请输入键盘的上下方向键来操作主菜单！！！\n\n",30,"");
                    system("pause");
                    system("cls");
                
                }
            }

            else if (ch == 13) {
                system("cls");

                switch (current) {
                case 1:
                    system("cls");
                    InputInfo();
                    break;
                case 2:
                    system("cls");
                    BrowseInfo();
                    break;
                case 3:
                    system("cls");
                    QueryInfo();
                    break;
                case 4:
                    system("cls");
                    ModifyInfo();
                    break;
                case 5:
                    system("cls");
                    DeleteInfo();
                    break;
                case 6:
                    system("cls");

                    printf("\n%*s谢谢使用！\n",30,"");
                    return 0;
                }
            }
            SaveData(); // 保存数据到文件
            Excels();   // 保存到excel
            
    }

    return 0;
}


// 输入订水信息函数
void InputInfo() {
    system("cls");
    printf("\n                                            ++++======== 信息录入 ========+++++\n");
    if (num_water_infos >= 1000) {
        printf("\n！！！已经录入1000个订水信息，无法继续录入！！！");
        return;
    }
 
    struct water_info info;
    memset(&info, 0, sizeof(info)); // 初始化结构体

    TimeInfo(&info);

    printf("\n%*s请输入姓名：",50,"");
    scanf("%s", info.name);
    printf("%*s请输入联系电话：",50,"");
    scanf("%s", info.phone);
    printf("%*s请输入订购数量：",50,"");
    scanf("%d", &info.amount);
    printf("%*s请输入金额：",50,"");
    scanf("%f", &info.money);
    printf("%*s请输入地址：", 50, "");
    scanf("%s", info.address);
    water_infos[num_water_infos++] = info;
    printf("\n%*s录入成功！\n",50,"");
    system("pause");
    system("cls");
}

// 浏览订水信息函数
void BrowseInfo() {
    system("cls");
    printf("\n%*s----======== 信息浏览 ========----",30,"");
    if (num_water_infos == 0) {
        printf("\n%*s没有任何订水信息。\n",30,"");
        system("pause");
        system("cls");
        return;
    }

    printf("\n\n\n姓名\t联系电话\t订购数量\t金额\t地址\t时间\n");
    for (int i = 0; i < num_water_infos; i++) {
        struct water_info info = water_infos[i];
        
        printf("\n%s\t%s\t\t%d\t\t%f\t\t%s\t%s\n\n",info.name, info.phone, info.amount, info.money, info.address, info.timestamp);
    }
    printf("\n");
    system("pause");
    system("cls");
}

// 查询订水信息函数
void QueryInfo() {
    system("cls");
    printf("\n%*s--======== 信息查询 ========--\n",50,"");
    if (num_water_infos == 0) {
        printf("\n%*s没有任何订水信息。\n",50,"");
        system("pause");
        system("cls");


        return;
        

    }
    char name[20];
    printf("\n%*s请输入要查询的姓名：",50,"");
    scanf("%s", name);

    int found = 0;
    for (int i = 0; i < num_water_infos; i++) {
        struct water_info info = water_infos[i];
        if (strcmp(info.name, name) == 0) {
            printf("\n\n\n姓名\t\t联系电话\t\t订购数量\t\t金额\t\t地址\n");
            printf("\n%s\t\t%s\t\t%d\t\t%f\t\t%s\n\n\n",info.name, info.phone, info.amount, info.money,info.address);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("\n%*s没有找到名为 %s 的订水信息。\n",50,"", name);
    }
    system("pause");
    system("cls");

}



// 修改订水信息函数
void ModifyInfo() {
    system("cls"); 

    printf("\n%*s-======== 信息修改 ========-\n",50,"");
    if (num_water_infos == 0) {
        printf("\n%*s没有任何订水信息。\n",50,"");
        system("pause");
        system("cls");
    }
    else {
        char name[20];
        printf("\n%*s请输入要修改的姓名：",50,"");
        scanf("%s", name);
        int found = 0;
        for (int i = 0; i < num_water_infos; i++) {
            struct water_info info = water_infos[i];
            if (strcmp(info.name, name) == 0) {
                printf("\n%*s原始信息：",50,"");
                printf("\n%*s姓名\t联系电话\t订购数量\t金额\t地址", 50, "");
                printf("\n%*s%s\t%s\t%d\t%f\t%s\n", 50, "", info.name, info.phone, info.amount,info.money, info.address);
                printf("\n%*s请输入新的联系电话：", 50, "");
                scanf("%s", info.phone);

                printf("%*s请输入新的订购数量：", 50, "");
                scanf("%d", &info.amount);

                printf("%*s请输入新的订购金额：", 50, "");
                scanf("%f", &info.money);

                printf("%*s请输入新的地址：", 50, "");
                scanf("%s", info.address);
                system("cls");

                water_infos[i] = info;
                printf("\n%*s修改成功！\n", 50, "");

                found = 1;
                break;
            }
        }

        if (!found) {
            printf("\n%*s没有找到名为 %s 的订水信息。\n", 50, "", name);
        }

        // 保存订水信息到文件中
        FILE* fp = fopen("water_info.dat", "w");
        if (fp == NULL) {
            printf("文件打开失败！\n");
            return;
        }
        else
        {
            printf("\n%*s订水信息已保存到文件中！\n", 50, "");
            
        }

        for (int i = 0; i < num_water_infos; i++) {
            struct water_info info = water_infos[i];
            fprintf(fp, "%s %s %d %f %c %c\n", info.name, info.phone, info.amount,info.money, info.address, info.timestamp);
        }
        fclose(fp);
        system("pause");
        system("cls");
    }
}



// 删除订水信息函数
void DeleteInfo() {
    system("cls");
    printf("\n%*s-======= 信息删除 =======-\n", 50, "");
    if (num_water_infos == 0) {
        printf("\n%*s没有任何订水信息。\n", 50, "");
        system("pause");
        system("cls");
        return;
    }
    else {
        char name[20];
        printf("\n%*s请输入要删除的姓名：", 50, "");
        scanf("%s", name);
        int found = 0;
        for (int i = 0; i < num_water_infos; i++) {
            struct water_info info = water_infos[i];
            if (strcmp(info.name, name) == 0) {
                for (int j = i; j < num_water_infos - 1; j++) {
                    water_infos[j] = water_infos[j + 1];
                }
                num_water_infos--;
                printf("\n%*s删除成功！\n", 50, "");
                found = 1;
                break;
            }
        }

        if (!found) {
            printf("\n%*s没有找到名为 %s 的订水信息。\n", 50, "", name);
        }

        // 保存订水信息到文件中
        FILE* fp = fopen("water_info.dat", "w");
        if (fp == NULL) {
            printf("%*s文件打开失败！\n", 50, "");
            return;
        }
        else
        {
            printf("\n%*s订水信息已保存到文件中！\n", 50, "");

        }

        for (int i = 0; i < num_water_infos; i++) {
            struct water_info info = water_infos[i];
            fprintf(fp, "%s %s %d %f %s\n", info.name, info.phone, info.amount,info.money, info.address);

        }

        fclose(fp);

        system("pause");
        system("cls");
    }
}

void TimeInfo(struct water_info* info) 
{
    time_t t = time(NULL);      
    struct tm* tm = localtime(&t);          // 获取当前时间
    strftime(info->timestamp, sizeof(info->timestamp), "%Y-%m-%d %H:%M:%S", tm);                
}


// 用户名和密码
const char USERNAME[] = "admin";
const char PASSWORD[] = "password";

int Login() {
    char username[20];
    char password[20];

    // 提示用户输入用户名和密码
    printf("%*s---========Login========---\n%*sUsername：",42,"",50,"");
    scanf("%s", username);

    printf("%*sPassword：",50,"");
    scanf("%s", password);

    // 验证用户名和密码
    if (strcmp(username, USERNAME) == 0 && strcmp(password, PASSWORD) == 0) {
        printf("%*s登陆成功！\n",50,"");
        system("pause");
        system("cls");
        return 0;
    }
    else {
        printf("%*s用户名或密码错误！\n",50,"");
        system("pause");
        system("cls");
        return 1;
    }
}

int Excels() {
    // 创建一个新的Excel文件
    BookHandle book = xlCreateBook();
    if (book == NULL) {
        printf("无法创建Excel文件！\n");
        return 1;
    }

    // 添加工作表
    SheetHandle sheet = xlBookAddSheet(book, "Sheet1", NULL);
    if (sheet == NULL) {
        printf("无法添加工作表！\n");
        xlBookRelease(book);
        return 1;
    }

    // 写入标签
    xlSheetWriteStr(sheet, 1, 0, "姓名", NULL);
    xlSheetWriteStr(sheet, 1, 1, "联系电话", NULL);
    xlSheetWriteStr(sheet, 1, 2, "订购数量", NULL);
    xlSheetWriteStr(sheet, 1, 3, "金额", NULL);
    xlSheetWriteStr(sheet, 1, 4, "地址", NULL);
    xlSheetWriteStr(sheet, 1, 5, "时间", NULL);
    
    // 写入用户数据
    for ( int i = 2; i < num_water_infos ; i++)
    {
        xlSheetWriteStr(sheet, i, 0, water_infos[i].name, NULL);
        xlSheetWriteStr(sheet, i, 1, water_infos[i].phone, NULL);
        xlSheetWriteNum(sheet, i, 2, water_infos[i].amount, NULL);
        xlSheetWriteNum(sheet, i, 3, water_infos[i].money, NULL);
        xlSheetWriteStr(sheet, i, 4, water_infos[i].address, NULL);
        xlSheetWriteStr(sheet, i, 5, water_infos[i].timestamp, NULL);
    }
    // 保存Excel文件
    if (!xlBookSave(book, "example.xls")) {
        printf("无法保存Excel文件！\n");
        xlBookRelease(book);
        return 1;
    }

    // 关闭Excel文件
    xlBookRelease(book);

    return 0;
}

